
var loadedImageCount = 0;
var page = 0;
var timer;
var diXian;
var st = cnt.scrollTop;
var ch = cnt.clientHeight;
var sh = cnt.scrollHeight;


cnt.onscroll = function () {
    var isDaoDi = (st + ch >= sh);
    if(isDaoDi&&!diXian){
            loadNextPageData();
        }

    };

loadNextPageData();

function loadNextPageData(){
     page++;
     if(page>=2){
         loading2.style.display = "block";
     }
     var pageCount = (page-1)*44;
    $.ajax({
        url:"https://s.taobao.com/search?data-key=s" +
        "&data-value="+pageCount+"&ajax=true" +
        "&_ksTS=1530243656560_891" +
        "&q=%E7%94%B7%E9%9E%8B&refpid=" +
        "430267_1006&source=tbsy&" +
        "style=grid&tab=all&" +
        "pvid=b8318040521666151a70a5f3eebd88c3" +
        "&clk1=0bf0718470c87df17b328af7d5f0e69e" +
        "&spm=a21bo.2017.201856-sline." +
        "2.5af911d9RRzslq&bcoffset=0" +
        "&p4ppushleft=3%2C44",
        dataType:"jsonp",
        success:function(data){
            loadData(data);
        }
    });
}

function loadData(data){
    if(!data.mods.itemlist.data){
        if(!diXian) {
            diXian = document.createElement("div");
            diXian.innerHTML = "我是有底线的";
            diXian.style.textAlign = "center";
            diXian.style.padding = "0.5rem";
            document.body.appendChild(diXian);
            loading2.style.display = "none";
        }
        d1.style.display = "none";
        return;
    }

    var productArr = data.mods.itemlist.data.auctions;
    for(var i=0;i<productArr.length;i++){
        var p = productArr[i];
        var productDiv = document.createElement("div");
        productDiv.className = "product fl";


        var jiaGeDiv = document.createElement("div");
        jiaGeDiv.className = "jiaGe";
        jiaGeDiv.innerHTML = "￥"+p.view_price;
        var fkDiv = document.createElement("div");
        fkDiv.className = "fk fr";
        fkDiv.innerHTML = p.view_sales;
        var image = document.createElement("img");
        image.src = "http:"+p.pic_url+"_360x360Q90.jpg_.webp";
        var bySpan= document.createElement("span");
        bySpan.className = "by";
        var titleDiv = document.createElement("div");
        titleDiv.className = "biaoti";
        titleDiv.style.height = "43px";
        titleDiv.style.overflow = "hidden";
        var text=document.createElement("span");
        text.innerHTML = p.title;

        var dpDiv = document.createElement("div");
        dpDiv.className = "dp";
        var dpm = document.createElement("span");
        dpm.innerHTML = p.nick;
        var shSpan = document.createElement("span");
        shSpan.className = "sh";
        var dsrSpan = document.createElement("span");
        dsrSpan.className = "dsr";
        var dsr1Span = document.createElement("span");
        dsr1Span.className = "dsr1";
        var dsr2Span = document.createElement("span");
        dsr2Span.className = "dsr2";



        productDiv.appendChild(image);
        productDiv.appendChild(fkDiv);
        titleDiv.appendChild(bySpan);
        titleDiv.appendChild(text);
        productDiv.appendChild(jiaGeDiv);

        productDiv.appendChild(dpDiv);
        dpDiv.appendChild(shSpan);
        dpDiv.appendChild(dpm);
        shSpan.appendChild(dsrSpan);
        shSpan.appendChild(dsr1Span);
        shSpan.appendChild(dsr2Span);
        productDiv.appendChild(titleDiv);

        productDiv.p = p;
        productDiv.onclick = function(){
            localStorage.setItem("currentP",JSON.stringify(this.p));
            location = "xqy.html";
        };

        productList.appendChild(productDiv);
    }
}
loadedImageCount = 0;

$("#productList img").load(
    function(){
        loadedImageCount++;

        // console.log(loadedImageCount);
        if(productArr.length==loadedImageCount){
            console.log("所有图片加载完毕！")
            // 在1秒内让该元素淡入出现
            $("#productList").fadeIn(400);
            //加载真实图片
            loadRealImage();
            d1.style.display = "none";

        }
    }
);

// 3.加载真实图片
function loadRealImage(){
    var images = productList.getElementsByTagName("img");
    // 这三十六张图
    // 首先俺们要显示在可视区域的图片

    var st = cnt.scrollTop;
    var ch = cnt.clientHeight;
    var sh = cnt.scrollHeight;

    // 某元素离顶部距离加上该元素的高度，如果这个结果小于 1136,
    // 该元素就完全在可视区域
    // pic 是一个普通图片元素

    for(var i=0;i<images.length;i++) {
        var picParent = images[i].parentNode;
        //eleheight 元素高度
        var eleheight = $(picParent).height();
        //eleOffsetTop 元素离顶部距离
        var eleOffsetTop = $(picParent).offset().top;

        if(eleheight+eleOffsetTop-st<ch){
            if(images[i].getAttribute("real_image_src")) {
                images[i].src = images[i].getAttribute("real_image_src");
                images[i].setAttribute("real_image_src", "");
                images[i].style.opacity = 1;
            }

        }
    }
}
$.init()